package com.pucheng.bracelet.viewmodel.bracelet

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.clj.fastble.exception.BleException
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.ble.BleCmd
import com.pucheng.bracelet.app.ble.BleWriteAndNotifyCallback
import com.pucheng.bracelet.app.ble.ClientManager
import com.pucheng.bracelet.app.util.ByteUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import com.pucheng.bracelet.ui.adapter.WatchData

class WatchViewModel : BaseViewModel() {

    //    val watchDatas: MutableLiveData<ListDataUiState<WatchData>> = MutableLiveData()
//
//    /**
//     * 获取屏保列表
//     */
//    fun getScreen() {
//        request({ apiService.getScreen() }, {
//            //请求成功
//            val dataUiState =
//                ListDataUiState(
//                    isSuccess = true,
//                    listData = it.lists
//                )
//            watchDatas.value = dataUiState
//        }, {
//            //请求失败
//            val dataUiState =
//                ListDataUiState(
//                    isSuccess = false,
//                    errMessage = it.errorMsg,
//                    listData = arrayListOf<WatchData>()
//                )
//            watchDatas.value = dataUiState
//        })
//    }
    private var data = mutableListOf<WatchData>().apply {
        add(WatchData(1, true, "", R.mipmap.watch0.toString(), "表盘1"))
        add(WatchData(2, false, "", R.mipmap.watch1.toString(), "表盘2"))
        add(WatchData(3, false, "", R.mipmap.watch2.toString(), "表盘3"))
    }
    var watchDatas = MutableLiveData<MutableList<WatchData>>().apply {
        value = data
    }

    fun getWatchData() {
        ClientManager.getInstance()
            .sendControlCmd(BleCmd.GET_CLOCK_DIAL ,
                object : BleWriteAndNotifyCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray?
                    ) {
//                    getServiceleepData(bleDevice)
                    }

                    override fun onWriteFailure(exception: BleException?) {
                    }

                    override fun onCharacteristicChanged(data: ByteArray?) {
//                LogUtils.e("资 = :${HexUtil.encodeHexStr(data)}")

                    }
                })
    }

    fun selectWatchData(index: Int) {
        ClientManager.getInstance()
            .sendControlCmd(BleCmd.SET_CLOCK_DIAL + ByteUtils.decToHex(index),
                object : BleWriteAndNotifyCallback() {
                    override fun onWriteSuccess(
                        current: Int,
                        total: Int,
                        justWrite: ByteArray?
                    ) {
//                    getServiceleepData(bleDevice)
                    }

                    override fun onWriteFailure(exception: BleException?) {
                        ToastUtils.showLong(R.string.updata_watch_face_fail)
                    }

                    override fun onCharacteristicChanged(data: ByteArray) {
                        if (data[0] == ByteUtils.intToByte(0xfb) && data[1] == ByteUtils.intToByte(
                                0x03
                            ) && data[2] == ByteUtils.intToByte(
                                0x01
                            )
                        ) {

                        }else{
                            ToastUtils.showShort(R.string.updata_watch_face_fail)
                        }
                    }
                })
    }

    fun selectWatchData(index: Int, context: Context) {
//        val filePath: String =
//            context.getExternalFilesDir(null)!!.path + File.separator + "RES_V1.4.bin"

        ClientManager.getInstance().upClockDial(context.assets.open("IdleRes4.bin").readBytes(),
            object : BleWriteAndNotifyCallback() {
                override fun onWriteSuccess(
                    current: Int,
                    total: Int,
                    justWrite: ByteArray?
                ) {
//                    getServiceleepData(bleDevice)
                }

                override fun onWriteFailure(exception: BleException?) {
                }

                override fun onCharacteristicChanged(data: ByteArray?) {
//                LogUtils.e("资 = :${HexUtil.encodeHexStr(data)}")

                }
            })
    }
}
